//
//  main.m
//  05-集合的循环遍历
//
//  Created by 曹曹 on 16/7/8.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
#pragma mark - 循环遍历
    // 定义一个数组, 包含5个字符串对象, 倒序取出数组中的所有元素, 并存储到另一个可变数组中
    NSArray *arr = @[@"张三", @"李四", @"王五", @"罗贯中", @"施耐庵"];
    NSMutableArray *newArr = [NSMutableArray array];
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        [newArr addObject:arr[i]];
    }
    NSLog(@"newArr:%@", newArr);
    // 定义一个字典, 包含一个人的name, age, sex三个数据, 完成对其三个数据的输出
    NSDictionary *dic = @{@"name": @"吴承恩",
                          @"age ": @24,
                          @"sex ": @"male"};
    NSArray *keys = dic.allKeys;
    for (NSInteger i = 0; i < keys.count; i++) {
        // NSNumber重写了Description方法
        id value = [dic objectForKey:keys[i]];
        NSLog(@"%@", value);
    }
#pragma mark - 枚举器
    // 数组的枚举器
    // NSEnumerator依赖于集合类型(数组, 字典, 集合), 不能单独使用
    NSEnumerator *stringEnumerator = [arr objectEnumerator];
    id value = nil;
    while (value = [stringEnumerator nextObject]) {
        NSLog(@"value = %@", value);
    }
    // 倒序枚举
    NSEnumerator *reverseEnumerator = [arr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverseEnumerator nextObject]) {
        NSLog(@"value1 = %@", value1);
    }
    // 字典的枚举器(字典无序, 没有倒序枚举)
    // 默认枚举的是value值, allKeys数组创建的枚举对象可以打印key值
    NSEnumerator *dicEnumerator = [dic objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicEnumerator nextObject]) {
        NSLog(@"value2 = %@", dicValue);
    }
    // 集合的枚举器(无序, 没有倒序枚举)
    NSSet *set = [NSSet setWithObjects:@"Hello", @"World", @"Beauty", @"Childhood", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"setValue = %@", setValue);
    }
#pragma mark - 快速枚举 for ... in (是对NSEnumerator的一层封装)
    // 定义一个数组, 包含5个字符串对象, 取出数组中所有的字符串, 并拼接生成一个新的字符串
    NSMutableString *appendString = [NSMutableString string];
    for (NSString *string in arr) {
        [appendString appendString:string];
    }
    NSLog(@"appendString = %@", appendString);
    // for ... in 倒序遍历!!!!!!!!!!!!!!!!!!!!!
    for (NSString *string in [arr reverseObjectEnumerator]) {
        NSLog(@"%@", string);
    }
    // 快速枚举一个字典
    for (NSString *dicString in dic) {
        NSLog(@"key   = %@", dicString);
        NSLog(@"value = %@", [dic objectForKey:dicString]);
    }
    // 快速枚举一个集合
    for (NSString *anyObj in set) {
        NSLog(@"anyObj = %@", anyObj);
    }
#pragma mark - 排序
    // 参数1 key: 排序条件
    // 参数2 acending: YES(升序), NO(降序)
    NSArray *array = @[@1, @2, @3,@4, @5];
    NSMutableArray *mArr = [NSMutableArray arrayWithObjects: @10, @11, @12, @13, @14, nil];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO];
    array = [array sortedArrayUsingDescriptors:@[sortDescriptor]];
    [mArr sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"sortedArray = %@", array);
    NSLog(@"mArr = %@", mArr);
    // 排序1 Descriptor
    // 可变数组和不可变数组通过sort排序调用的方法不同
    // 1.不可变数组, 排序后需要生成新的数组
    // 2.可变数组, 排序可以直接在本身之上修改
    Person *per1 = [[Person alloc] initWithName:@"zhangsan" age:23];
    Person *per2 = [[Person alloc] initWithName:@"wangzeli" age:24];
    Person *per3 = [[Person alloc] initWithName:@"aniyakai" age:35];
    Person *per4 = [[Person alloc] initWithName:@"holyholy" age:30];
    NSArray *personArray = [NSArray arrayWithObjects:per1, per2, per3, per4, nil];
    NSLog(@"persArray = %@", personArray);
    NSSortDescriptor *perDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];
    personArray = [personArray sortedArrayUsingDescriptors:@[perDescriptor]];
    NSLog(@"sortArray = %@", personArray);
    // 排序2 selector
    NSArray *selArray = @[@"iPhone", @"iPad", @"iTouch", @"MacBookPro", @"MacMini", @"Android"];
    selArray = [selArray sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"selArray = %@", selArray);
    return 0;
}
